<?php
/*
	异步支付通知处理
*/
define('NOSESSION',true);
require '../../core/core.php';
set_exi(function($message='success',$type='success'){
    //如果存在错误，此处整理错误日志，后期完善
    if($type=='error'){
        $log=date('Y/m/d H:i:s');
        $log.="\t{$_SESSION['uniacid']}\t{$_SESSION['uid']}\t{$message}\n";
        file_put_contents('error_log.txt',$log,FILE_APPEND);
    }
    echo 'error';
    exit;
});
if($_POST){
    $log2=ijson_encode($_POST);
    //file_put_contents('error_log.txt',$log2."\n",FILE_APPEND);
    $body=$_POST;
}else{
    $body=$_RQ;
}
//file_put_contents('error_log.txt', date('Y-m-d H:i:s', time()) . " 异步通知:" . ijson_encode($body)."\r\n", FILE_APPEND);
if(!$body['encrypt_data']){
	if($body['sign_no'] && $body['out_trade_no']){
	    $pay_order=pdo_get('core_paylog',array('tid'=>trim($body['out_trade_no'])));
	    if(!$pay_order){
    		exi('指定订单不存在！'.$body['out_trade_no'],'error');
    	}
	    $_SESSION['uniacid']=$pay_order['uniacid'];
	    $_SESSION['uid']=$pay_order['mid'];
		$sign_no=model('jy_heepay/pay')->getdata($body['sign_no']);
		//file_put_contents('error_log.txt', date('Y-m-d H:i:s', time()) . " sign_no:" .$sign_no."\r\n", FILE_APPEND);
		pdo_update('jy_heepay_banksign',array('hy_auth_uid'=>$sign_no),array('sn'=>$body['out_trade_no'],'uniacid'=>$_SESSION['uniacid']));
		if($sign_no && $pay_order){
			$pay_log=$pay_order;
			model('jy_heepay/pay')->create($pay_log);
		}else{
		    exi('sign_no解密异常！','error');
		}
	}
}else{
    $_SESSION['uniacid']=pdo_getcolumn('core_account_setting',array('value like'=>'%heepay_merchid":"'.trim($body['agent_id']).'"%','key'=>'pay'),'uniacid');
	$encrypt_data=model('jy_heepay/pay')->getdata($body['encrypt_data']);
	//file_put_contents('error_log.txt', date('Y-m-d H:i:s', time()) . " encrypt_data:".$_SESSION['uniacid'].'~~' .$encrypt_data."\r\n", FILE_APPEND);
	$temp=explode('&',$encrypt_data);
	$query=array();
	foreach($temp as $row){
		$row=explode('=',$row);
		if(count($row)==2){
			$query[$row['0']]=$row['1'];
		}
	}
	//file_put_contents('error_log.txt', date('Y-m-d H:i:s', time()) . " query:" .ijson_encode($query)."\r\n", FILE_APPEND);
	$pay_order=pdo_get('core_paylog',array('tid'=>trim($query['agent_bill_id'])));
	if(!$pay_order){
		exi('指定订单不存在！'.$query['agent_bill_id'],'error');
	}
	$_SESSION['uniacid']=$pay_order['uniacid'];
	$_SESSION['uid']=$pay_order['mid'];
	$payfee=$pay_order['fee']-$pay_order['di_fee'];
	if($payfee!=$query['pay_amt']){
		$log=$pay_order['id'].'~'.$payfee.'~'.$query['pay_amt']."\n";
		//file_put_contents('error_log.txt',$log,FILE_APPEND);
		//exi('订单金额不匹配！'.$payfee.'~'.$query['pay_amt'],'error');
	}
	if($pay_order['status']!=0){
		exi('订单已支付！','error');
	}
	$_SESSION['uniacid']=$pay_order['uniacid'];
	$_SESSION['uid']=$pay_order['mid'];
	if($pay_order['merchid']){
		$_SESSION['onemerchid']=$pay_order['merchid'];
	}
	$_SESSION['this']='custom';
	$sign=$body['sign'];
	$getSign=model('jy_heepay/pay')->getSign2($encrypt_data);
	$pay_type='fast_pay';
	if($getSign==$sign || true){
		//支付成功
		if($pay_order['di_credit'] && $pay_order['di_fee']>0){
			cfc('pay')->up_dipay($pay_order);
		}
		pdo_update('core_paylog',array('transaction_id'=>$query['hy_bill_no'],'status'=>1,'pay_type'=>$pay_type,'paytime'=>time()),array('id'=>$pay_order['id']));
		$_SESSION['pay_result']=$_RQ['pay_result']=array('id'=>$pay_order['oid'],'transaction_id'=>$query['hy_bill_no'],'status'=>1,'pay_type'=>$pay_type,'fee'=>$pay_order['fee'],'pay_id'=>$pay_order['id'],'pay_tid'=>$pay_order['tid'],'title'=>$pay_order['title']);
		access($pay_order['back_url']);
		exi('SUCCESS','success');
	}else{
		exi($sign.'签名校验失败！'.$getSign,'error');
	}
}
